package cz.cernilovsky.android.profileapp.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.app.PendingIntent;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener;
import com.google.android.gms.location.LocationClient.OnRemoveGeofencesResultListener;

import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.model.Location;
import cz.cernilovsky.android.profileapp.storage.LocationsStorage;

public class RemoveGeofence implements ConnectionCallbacks, OnConnectionFailedListener, OnRemoveGeofencesResultListener{
	private Location location;
	private LocationClient c;
	private Context context;
	private static final String TAG = "RemoveGeofence";
	private boolean REMOVE_ALL = false;
	
	public static void removeLocation(Location location, Context context) {
		new RemoveGeofence(location, context);
	}
	
	public static void removeAllLocations(Context context) {
		new RemoveGeofence(context);
	}
	
	private RemoveGeofence(Location location, Context context) {
		this.location = location;
		this.context = context;
		REMOVE_ALL = false;
		c = new LocationClient(context, this, this);
		c.connect();
	}
	
	private RemoveGeofence(Context context) {
		this.context = context;
		REMOVE_ALL = true;
		c = new LocationClient(context, this, this);
		c.connect();
	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		ToastService.toast(context, R.string.connection_failed);
		Log.e(TAG, "Connection failed");
	}

	@Override
	public void onConnected(Bundle arg0) {
		if(!REMOVE_ALL) {
			List<String> geofenceRequestIds = new ArrayList<String>();
			geofenceRequestIds.add(String.valueOf(location.getId()));
			c.removeGeofences(geofenceRequestIds, this);
		} else {
			try {
				Location[] locations = LocationsStorage.loadLocations(context);
				if(locations != null) {
					List<String> ids =  new ArrayList<String>();
					for(Location l : locations) {
						ids.add(String.valueOf(l.getId()));
					}
					c.removeGeofences(ids, this);
				}
			} catch (IOException e) {
				NotificationService.notify(context, R.string.error_IO_unregister, R.string.error);
				e.printStackTrace();
			} catch (JSONException e) {
				NotificationService.notify(context, R.string.error_JSON_unregister, R.string.error);
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onDisconnected() {
		ToastService.toast(context, R.string.error_location_service_disconnected);
		Log.e(TAG, "Connection failed");
	}

	@Override
	public void onRemoveGeofencesByPendingIntentResult(int arg0,
			PendingIntent arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRemoveGeofencesByRequestIdsResult(int arg0, String[] arg1) {
		if(!REMOVE_ALL) {
			ToastService.toast(context, context.getString(R.string.location_removed) + ": " + location.getName());
			//Preferences.removeLocationID(context, arg1[0]);
			NotificationService.mainNotification(context);
		}
		c.disconnect();
	}

}
